package ynu.edu.online_course.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ynu.edu.online_course.entity.Chapter;
import ynu.edu.online_course.service.ChapterService;

@RestController
@RequestMapping("/chapters")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @GetMapping("/course/{courseId}")
    public ResponseEntity<?> getChaptersByCourseId(@PathVariable Long courseId) {
        return ResponseEntity.ok(chapterService.getChaptersByCourseId(courseId));
    }

    @PostMapping
    public ResponseEntity<?> addChapter(@RequestBody Chapter chapter) {
        chapterService.addChapter(chapter);
        return ResponseEntity.ok("Chapter added successfully");
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> updateChapter(@PathVariable Long id, @RequestBody Chapter chapter) {
        chapter.setId(id);
        chapterService.updateChapter(chapter);
        return ResponseEntity.ok("Chapter updated successfully");
    }

    @GetMapping("/parent/{parentId}")
    public ResponseEntity<?> getSubChapters(@PathVariable Long parentId) {
        return ResponseEntity.ok(chapterService.getSubChapters(parentId));
    }
}
